'''
Created on Jan 11, 2010

@author: Berin
@author: Alex
'''
import sys
import pygame

from pygame.locals import * 
class InputManager(object):
	'''
	Manages input events
	'''


	def __init__(self):
		'''
		Constructor
		'''
		self.is_quitting_pressed = False 
		self.is_ctrl_pressed = False
		self.is_alt_pressed = False
		
		
		self.left_tile = {K_w: ['Y', 1], K_s: ['Y', -1], K_a: ['X', 1], K_d: ['X', -1]}
		self.right_tile = {K_UP: ['Y', 1], K_DOWN: ['Y', -1], K_LEFT: ['X', 1], K_RIGHT: ['X', -1]}
		
		self.mouse_scroll =       {4: ['Y', 1], 5: ['Y', -1]}
		self.mouse_scroll_ctrl =  {4: ['X', 1], 5: ['X', -1]}
		
		self.mouse_pressed = {'button': [1, self.is_alt_pressed]}
		
	def process(self, game):
		'''
		Processes the various input events
		'''
		
		self.mouseX, self.mouseY = game.mouse.get_pos()
		
		events = game.event.get()
		#print events
		for event in events:
			#self.mouse_checker()
			if event.type == KEYUP and event.key == K_ESCAPE or event.type == QUIT:
				self.is_quitting_pressed = True
				game.quit()
				sys.exit(0)
				
			elif event.type == KEYDOWN:
				
				if event.key == K_LCTRL or event.key == K_RCTRL:
					self.is_ctrl_pressed = True
					
				if self.is_ctrl_pressed:
					
					if event.key == K_s:
						return {type: 'file', input: 'save'}
					
					if event.key == K_l:
						return {type: 'file', input: 'load'}
					
				if event.key == K_LALT or event.key == K_RALT:
					self.is_alt_pressed = True
					
				if event.key == K_w or event.key == K_s or event.key == K_a or event.key == K_d:
					return {type: 'left_tile', input: self.left_tile[event.key]}
					
				if event.key == K_UP or event.key == K_DOWN or event.key == K_LEFT or event.key == K_RIGHT:
					return {type: 'right_tile', input: self.right_tile[event.key]}

			elif event.type == KEYUP:
				if event.key == K_LCTRL or event.key == K_RCTRL:
					self.is_ctrl_pressed = False
				if event.key == K_LALT or event.key == K_RALT:
					self.is_alt_pressed = False
			
			elif event.type == MOUSEBUTTONDOWN:
				if event.button == 1 or event.button == 3:
					return {type: 'mouse_down', 'pressed': self.is_alt_pressed , 'button': 'click'}
				elif event.button == 4 or event.button == 5:
					if self.is_ctrl_pressed:
						return {type: 'mouse_down', input: self.mouse_scroll_ctrl[event.button], 'button': 'scroll'}
					else:
						return {type: 'mouse_down', input: self.mouse_scroll[event.button], 'button': 'scroll'}
			
			elif event.type == VIDEORESIZE:
				width = event.w
				height = event.h
				if width < 800:
					width = 800
				if height < 600:
					height = 600	
				return {type: 'resize', input: [width, height]}	
				
		return {type: None, input: None}